package o;

import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Looper;
import android.os.Message;
import androidx.annotation.Nullable;
import com.google.android.exoplayer2.ExoPlaybackException;
import com.google.android.exoplayer2.Format;
import com.google.android.exoplayer2.text.Cue;
import java.util.Collections;
import java.util.List;

public final class ajk extends aay implements Callback {
    private ajf ʻ;
    private int ʼ;
    private Format ʽ;
    private boolean ˊ;
    private final ajn ˋ;
    private int ˋॱ;
    private final ajm ˎ;
    @Nullable
    private final Handler ˏ;
    private ajj ˏॱ;
    private ajj ͺ;
    private final abk ॱ;
    private ajl ॱॱ;
    private boolean ᐝ;

    public ajk(ajn o_ajn, @Nullable Looper looper) {
        this(o_ajn, looper, ajm.ˏ);
    }

    public ajk(ajn o_ajn, @Nullable Looper looper, ajm o_ajm) {
        Handler handler;
        super(3);
        this.ˋ = (ajn) amm.ॱ(o_ajn);
        if (looper == null) {
            handler = null;
        } else {
            handler = anr.ˏ(looper, (Callback) this);
        }
        this.ˏ = handler;
        this.ˎ = o_ajm;
        this.ॱ = new abk();
    }

    public int ˎ(Format format) {
        if (this.ˎ.ˋ(format)) {
            return aay.ˎ(null, format.ʻ) ? 4 : 2;
        } else {
            if (anb.ˊ(format.ʽ)) {
                return 1;
            }
            return 0;
        }
    }

    public void ˏ(Format[] formatArr, long j) throws ExoPlaybackException {
        this.ʽ = formatArr[0];
        if (this.ʻ != null) {
            this.ʼ = 1;
        } else {
            this.ʻ = this.ˎ.ˏ(this.ʽ);
        }
    }

    public void ˏ(long j, boolean z) {
        ˊᐝ();
        this.ˊ = false;
        this.ᐝ = false;
        if (this.ʼ != 0) {
            ʿ();
            return;
        }
        ʾ();
        this.ʻ.ˋ();
    }

    public void ˊ(long j, long j2) throws ExoPlaybackException {
        if (!this.ᐝ) {
            if (this.ˏॱ == null) {
                this.ʻ.ˏ(j);
                try {
                    this.ˏॱ = (ajj) this.ʻ.ˏ();
                } catch (Exception e) {
                    throw ExoPlaybackException.ˊ(e, ॱᐝ());
                }
            }
            if (ˊ() == 2) {
                boolean z;
                if (this.ͺ != null) {
                    long ˋˊ = ˋˊ();
                    z = false;
                    while (ˋˊ <= j) {
                        this.ˋॱ++;
                        ˋˊ = ˋˊ();
                        z = true;
                    }
                } else {
                    z = false;
                }
                if (this.ˏॱ != null) {
                    if (this.ˏॱ.ˏ()) {
                        if (!z && ˋˊ() == Long.MAX_VALUE) {
                            if (this.ʼ == 2) {
                                ʿ();
                            } else {
                                ʾ();
                                this.ᐝ = true;
                            }
                        }
                    } else if (this.ˏॱ.ˊ <= j) {
                        if (this.ͺ != null) {
                            this.ͺ.ˋ();
                        }
                        this.ͺ = this.ˏॱ;
                        this.ˏॱ = null;
                        this.ˋॱ = this.ͺ.ॱ(j);
                        z = true;
                    }
                }
                if (z) {
                    ॱ(this.ͺ.ˊ(j));
                }
                if (this.ʼ != 2) {
                    while (!this.ˊ) {
                        try {
                            if (this.ॱॱ == null) {
                                this.ॱॱ = (ajl) this.ʻ.ॱ();
                                if (this.ॱॱ == null) {
                                    return;
                                }
                            }
                            if (this.ʼ == 1) {
                                this.ॱॱ.ˎ(4);
                                this.ʻ.ˏ(this.ॱॱ);
                                this.ॱॱ = null;
                                this.ʼ = 2;
                                return;
                            }
                            int ˊ = ˊ(this.ॱ, this.ॱॱ, false);
                            if (ˊ == -4) {
                                if (this.ॱॱ.ˏ()) {
                                    this.ˊ = true;
                                } else {
                                    this.ॱॱ.ˊ = this.ॱ.ॱ.ˊॱ;
                                    this.ॱॱ.ᐝ();
                                }
                                this.ʻ.ˏ(this.ॱॱ);
                                this.ॱॱ = null;
                            } else if (ˊ == -3) {
                                return;
                            }
                        } catch (Exception e2) {
                            throw ExoPlaybackException.ˊ(e2, ॱᐝ());
                        }
                    }
                }
            }
        }
    }

    public void ॱˎ() {
        this.ʽ = null;
        ˊᐝ();
        ʼॱ();
    }

    public boolean ʽॱ() {
        return this.ᐝ;
    }

    public boolean ˈ() {
        return true;
    }

    private void ʾ() {
        this.ॱॱ = null;
        this.ˋॱ = -1;
        if (this.ͺ != null) {
            this.ͺ.ˋ();
            this.ͺ = null;
        }
        if (this.ˏॱ != null) {
            this.ˏॱ.ˋ();
            this.ˏॱ = null;
        }
    }

    private void ʼॱ() {
        ʾ();
        this.ʻ.ˎ();
        this.ʻ = null;
        this.ʼ = 0;
    }

    private void ʿ() {
        ʼॱ();
        this.ʻ = this.ˎ.ˏ(this.ʽ);
    }

    private long ˋˊ() {
        if (this.ˋॱ == -1 || this.ˋॱ >= this.ͺ.ॱ()) {
            return Long.MAX_VALUE;
        }
        return this.ͺ.ˊ(this.ˋॱ);
    }

    private void ॱ(List<Cue> list) {
        if (this.ˏ != null) {
            this.ˏ.obtainMessage(0, list).sendToTarget();
        } else {
            ˋ(list);
        }
    }

    private void ˊᐝ() {
        ॱ(Collections.emptyList());
    }

    public boolean handleMessage(Message message) {
        switch (message.what) {
            case 0:
                ˋ((List) message.obj);
                return true;
            default:
                throw new IllegalStateException();
        }
    }

    private void ˋ(List<Cue> list) {
        this.ˋ.ˎ(list);
    }
}
